package com.controller.system;

import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.utils.EntityConversionUtil;
import com.github.pagehelper.PageInfo;
import com.model.base.PageResult;
import com.model.base.Result;
import com.model.domain.system.GetSystemDomainInfoDO;
import com.model.dto.system.domain.DeleteDomainInfoDTO;
import com.model.dto.system.domain.GetDomainListDTO;
import com.model.dto.system.domain.SaveDomainInfoDTO;
import com.model.pojo.system.SystemDomainInfo;
import com.model.vo.system.domain.getDomainList.GetDomainListVO;
import com.service.device.DeviceObdBaseInfoService;
import com.service.system.SystemDomainInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * TODO
 *
 * @Description
 * @Author tm
 * @Date 2023-11-22 11:06
 **/
@Api(tags = {"域名管理"})
@Slf4j
@RestController
@RequestMapping("/v1/system/domain")
@RequiredArgsConstructor
public class SystemDomainInfoController {
    private final SystemDomainInfoService systemDomainInfoService;
    private final DeviceObdBaseInfoService deviceObdBaseInfoService;
    @Log(title = "域名管理-域名列表", businessType = BusinessType.SELECT)
    @ApiOperation(value = "域名列表")
    @PostMapping("/getDomainList")
    public Result<PageResult<List<GetDomainListVO>>> getDomainList(@RequestBody @Valid GetDomainListDTO dto){
        List<GetDomainListVO> voList = new ArrayList<GetDomainListVO>();
        GetSystemDomainInfoDO getSystemDomainInfoDO = EntityConversionUtil.copy(dto,GetSystemDomainInfoDO.class);
        getSystemDomainInfoDO.setSortRow("createTime");
        List<SystemDomainInfo> list = systemDomainInfoService.getPageList(getSystemDomainInfoDO);
        if(list!=null && list.size()>0){
            voList = EntityConversionUtil.copyList(list,GetDomainListVO.class);
        }
        PageInfo<SystemDomainInfo> pageInfo = new PageInfo<>(list);
        PageResult<List<GetDomainListVO>> build = PageResult.<List<GetDomainListVO>>builder().result(voList).pageInfo(pageInfo).build();
        return Result.suc(build);
    }

    @Log(title = "域名管理-保存域名", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "保存域名")
    @PostMapping("/saveDomainInfo")
    public Result<Objects> saveDomainInfo(@RequestBody @Valid SaveDomainInfoDTO dto){
        int num = 0;
        if(dto.getId()!=null && dto.getId()!=0){
            SystemDomainInfo info = systemDomainInfoService.getById(dto.getId());
            if(info == null){
                return Result.fail("9999","当前记录不存在");
            }
            long count = deviceObdBaseInfoService.countByDomainId(dto.getId());
            if(count>0){
                return Result.fail("9999","当前域名有设备关联");
            }
            info.setDomain(dto.getDomain());
            info.setPort(dto.getPort());
            info.setRemark(dto.getRemark());
            info.setType(dto.getType());
            num = systemDomainInfoService.update(info);
        }else{
            SystemDomainInfo info = EntityConversionUtil.copy(dto,SystemDomainInfo.class);
            num = systemDomainInfoService.save(info);
        }
        if(num>0){
            return Result.suc();
        }
        return Result.fail("9999","保存失败");
    }

    @Log(title = "域名管理-删除域名", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除域名")
    @PostMapping("/deleteDomainInfo")
    public Result<Objects> deleteDomainInfo(@RequestBody @Valid DeleteDomainInfoDTO dto){
        int num = 0;
        if(dto.getId()!=null && dto.getId()!=0){
            SystemDomainInfo info = systemDomainInfoService.getById(dto.getId());
            if(info == null){
                return Result.fail("9999","当前记录不存在");
            }
            long count = deviceObdBaseInfoService.countByDomainId(dto.getId());
            if(count>0){
                return Result.fail("9999","当前域名有设备关联");
            }
            num = systemDomainInfoService.delete(dto.getId());
        }
        if(num>0){
            return Result.suc();
        }
        return Result.fail("9999","删除失败");
    }
}
